package com.cn.longdistancebusstation.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.callback.OnCouponListener;
import com.cn.longdistancebusstation.model.Coupons;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mucheng on 2018/2/26.
 */

public class CouponAdapter extends BaseAdapter{
    private Context context;
    private List<Coupons> mCouponsList;
    private String strCouponId;
    List<Boolean> mChecked;
    private String couponId;

    public void setCouponsList(List<Coupons> couponsList) {
        mCouponsList = couponsList;

    }

    LayoutInflater mLayoutInflater;
    private int coupon;
    private BigDecimal mCoupon;
    private BigDecimal mBigDecimal;
    private OnCouponListener mOnCouponListener;
    public CouponAdapter(Context context,List<Coupons> mCouponsList,String strCouponId){
        this.context=context;
        mLayoutInflater = LayoutInflater.from(context);
        if(mCouponsList!=null){
            this.mCouponsList=mCouponsList;
            strCouponId=strCouponId;
        }else{
            this.mCouponsList=new ArrayList<>();
        }


    }



    @Override
    public int getCount() {
        return mCouponsList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCouponsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        final int pos = i;
        if(view==null){
            holder=new ViewHolder();
            view= mLayoutInflater.inflate(R.layout.view_coupons2,null);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        holder.mTicketCouponTextView=(TextView)view.findViewById(R.id.discount_detail);
        holder.mRangeOfApplicationIllustrateTextView = (TextView)view.findViewById(R.id.range_of_application_illustrate);
        holder.mPeriodOfValidityTextView=(TextView)view.findViewById(R.id.period_of_validity_illustrate);
        holder.mCheckBox=(CheckBox)view.findViewById(R.id.coupon_select);
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                Log.e("DAyinshuju", "onCheckedChanged: "+compoundButton+b );
//                holder.mCheckBox.setChecked(mCouponsList.get(pos).isChecked());
                mOnCouponListener.onClick(pos);




            }
        });
        holder.mCouponLinearLayout=(LinearLayout)view.findViewById(R.id.couponLayout);

        Coupons coupons=mCouponsList.get(pos);
        String price = coupons.getCouponX();
        mCoupon = new BigDecimal(price);
        String strCoupon =mCoupon.multiply(new BigDecimal(10)).setScale(1).toString();

        holder.mTicketCouponTextView.setText(strCoupon+"折");
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        String currentDate=formatter.format(new java.util.Date());
        Log.d("当前时间",currentDate);
        String strPeriodOfValidity=coupons.getEffectiveTime();
        String strTime=coupons.getStartDate();
        Date time1=null;
        int time2=Integer.parseInt(strPeriodOfValidity);
        Date curentTime=null;
        try {
            time1 = formatter.parse(strTime);
            curentTime=formatter.parse(currentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date newDate2 = new Date(time1.getTime() + time2 * 24 * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateOk = simpleDateFormat.format(newDate2);
        holder.mPeriodOfValidityTextView.setText(dateOk);
        if(time1.getTime()>curentTime.getTime()){

        }else{

        }
        return view;
    }
    public void setOnCouponListener(OnCouponListener onCouponListener){
        mOnCouponListener=onCouponListener;
    }
    class ViewHolder{
        TextView mTicketCouponTextView;
        TextView mRangeOfApplicationIllustrateTextView;
        TextView mPeriodOfValidityTextView;
        CheckBox mCheckBox;
        LinearLayout mCouponLinearLayout;
    }
}
